﻿namespace Sai.Test.Introspection
{
	using System.Linq;
	using NSubstitute;
	using Sai.Core.Introspection;
	using Xunit;
    using Sai.Test.XUnit;

	public class ITypeDeclarationCollectionFacts
	{
		public class TheInCommonWithMethod
		{
			
			[Fact, InMemoryTest]
			public void ReturnsAllItemsForIdenticalCollections()
			{
				ITypeDeclaration firstType = Substitute.For<ITypeDeclaration>();
				firstType.TypeName.Returns(new TypeName(new FullyQualifiedName("System.Object")));

				TypeDeclarationCollection first = new TypeDeclarationCollection();
				first.Add(firstType);

				ITypeDeclaration secondType = Substitute.For<ITypeDeclaration>();
				secondType.TypeName.Returns(new TypeName(new FullyQualifiedName("System.Object")));

				TypeDeclarationCollection second = new TypeDeclarationCollection();
				second.Add(secondType);

				var collection = second.InCommonWith(first);

				Assert.Equal(1, collection.Count);
				Assert.Equal(new TypeName(new FullyQualifiedName("System.Object")), collection.First());
			}

			
			[Fact, InMemoryTest]
			public void ReturnsOnlyCommonItemsForMixedCollections()
			{
				ITypeDeclaration firstType = Substitute.For<ITypeDeclaration>();
				firstType.TypeName.Returns(new TypeName(new FullyQualifiedName("System.Object")));
				ITypeDeclaration secondType = Substitute.For<ITypeDeclaration>();
				secondType.TypeName.Returns(new TypeName(new FullyQualifiedName("System.String")));
				ITypeDeclaration thirdType = Substitute.For<ITypeDeclaration>();
				thirdType.TypeName.Returns(new TypeName(new FullyQualifiedName("Example.TestClass")));

				TypeDeclarationCollection first = new TypeDeclarationCollection();
				first.Add(firstType);
				first.Add(secondType);
				first.Add(thirdType);

				ITypeDeclaration fourthType = Substitute.For<ITypeDeclaration>();
				fourthType.TypeName.Returns(new TypeName(new FullyQualifiedName("System.Object")));
				ITypeDeclaration fifthType = Substitute.For<ITypeDeclaration>();
				fifthType.TypeName.Returns(new TypeName(new FullyQualifiedName("Example.NotInFirst")));
				ITypeDeclaration sixthType = Substitute.For<ITypeDeclaration>();
				sixthType.TypeName.Returns(new TypeName(new FullyQualifiedName("Example.TestClass")));

				TypeDeclarationCollection second = new TypeDeclarationCollection();
				second.Add(fourthType);
				second.Add(fifthType);
				second.Add(sixthType);

				var collection = second.InCommonWith(first);

				Assert.Equal(2, collection.Count);
			}
		}
	}
}
